import pymysql
"""
连接数据库
"""


def get_conn():
    # 建立连接
    conn = pymysql.connect(host="localhost", user="root", password="123456", db="cov", charset="utf8")
    # 创建游标
    cursor = conn.cursor()
    # print(conn)
    return conn, cursor


"""
断开数据库连接
"""


def close_conn(conn, cursor):
    if cursor:
        cursor.close()
    if conn:
        conn.close()


"""
获取历史每日新增数据
"""


def getAddHistory():
    conn, cursor = get_conn()
    sql = "SELECT * FROM chinadayadd_history"

    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 获取所有记录列表
        results = cursor.fetchall()
        x_list = []
        y_list = []
        i = 0
        while i < len(results)-1:
            confirm = results[i+1][2]
            dead = results[i][4]
            heal = results[i][5]
            x_list.append([dead, heal])
            y_list.append(confirm)
            i = i+1
        print("i="+str(i))
        lastData = [[results[i][4], results[i][5]]]
    except Exception as e:
        print(e)
        print("Database is error,don't get data")
    close_conn(conn, cursor)

    return x_list, y_list, lastData


"""
获取各省当日情况
"""


def get_provinceday():
    conn, cursor = get_conn()
    sql = "SELECT * FROM provincetoday"
    name_list = []
    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 获取所有记录列表
        results = cursor.fetchall()
        pro_list = []
        for row in results:
            name = row[0]
            confirm = row[1]
            dead = row[3]
            heal = row[4]
            pro_list.append([confirm, dead, heal])
            name_list.append(name)
    except:
        print("Database is error,don't get data")
    close_conn(conn, cursor)
    print(pro_list)
    return name_list, pro_list


if __name__ == '__main__':
    # getAddHistory()
    get_provinceday()
    pass
